package com.simtop.security.handle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.simtop.utils.iputil.ServletUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * @ClassName MyAccessDeniedHandler
 * @Description TODO   自定义无权限结果返回
 *    无权限处理,即无权限时返回
 * @Author lvyongwei
 * @Date
 **/
@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler, Serializable {

    private static final long serialVersionUID = -8970718410437077606L;

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        String msg = "不好意思，您无访问权限";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",413);
        jsonObject.put("msg",msg);
        ServletUtils.renderString(httpServletResponse, JSON.toJSONString(jsonObject));
    }
}
